package cn.gailvlun.gll.presentation.apartment.room.vm

import android.arch.lifecycle.ViewModel
import cn.gailvlun.gll.chatlib.db.ChatDB2
import cn.gailvlun.gll.net.Member
import io.realm.Realm


class IPromiseViewModel: ViewModel {

    private var realm: Realm?
    private var member: Member? = null

    constructor() : super() {
        realm = Realm.getInstance(ChatDB2.getConfiguration())
    }

    fun getMemberById(memberId: Int){
        this.member = realm!!.where(Member::class.java)
                .equalTo("id", memberId)
                .findFirst()
    }

    fun getMemberId(): Int? {
        return this.member?.id
    }

    fun saveMember(member: Member) {
        realm!!.executeTransaction { realm ->
            realm.insertOrUpdate(member)
        }
    }

    override fun onCleared() {
        realm?.close()
        realm = null

        super.onCleared()
    }
}